package com.saishi.dao.impl;

import com.saishi.dao.BooksQuestionDao;
import com.saishi.entity.BooksQuestion;
import com.saishi.util.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

public class BooksQuestionDaoImpl implements BooksQuestionDao {
    QueryRunner qr = new QueryRunner(AliPool.getDs());

    @Override
    public int insert(BooksQuestion booksQuestion) throws SQLException {
        String sql = "INSERT INTO `booksquestion` VALUES(null,?,?);";
        return qr.update(sql, booksQuestion.getBqquestionid(), booksQuestion.getBqbookid());
    }

    @Override
    public Long count(int bookid) throws SQLException {
        String sql = "SELECT count(*) FROM `booksquestion` where bqbookid = ?;";
        return qr.query(sql, new ScalarHandler<Long>(), bookid);
    }

    @Override
    public int[] delete(Object[][] obj) throws SQLException {
        String sql = "DELETE FROM `booksquestion` WHERE bqquestionid = ?;";
        return qr.batch(sql, obj);
    }

    @Override
    public int deleteBook(int bookid) throws SQLException {
        String sql = "DELETE FROM `booksquestion` WHERE `bqquebqbookidstionid` = ?;";
        return qr.update(sql, bookid);
    }

    @Override
    public int[] deleteOne(Object[][] obj) throws SQLException {
        String sql = "DELETE FROM `booksquestion` WHERE `bqquestionid` = ? and `bqbookid` = ?;";
        return qr.batch(sql, obj);
    }

    @Override
    public int[] insert(Object[][] obj) throws SQLException {
        String sql = "INSERT INTO `booksquestion` VALUES (null, ?, ?);";
        return qr.batch(sql, obj);
    }
}
